# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("@com_google_googleapis_imports//:imports.bzl", "cc_proto_library")

package(
    default_visibility = ["//visibility:public"],
    licenses = ["notice"],
)

cc_library(
    name = "references",
    srcs = ["references.cc"],
    hdrs = ["references.h"],
    # Disable default arguments internally. Using them in PDPI itself is very likely a bug.
    local_defines = ["PDPI_DISABLE_TRANSLATION_OPTIONS_DEFAULT"],
    deps = [
        ":built_ins",
        ":ir_cc_proto",
        ":reference_annotations",
        "//gutil/gutil:collections",
        "//gutil/gutil:proto",
        "//gutil/gutil:status",
        "//p4_pdpi/string_encodings:byte_string",
        "@com_github_p4lang_p4runtime//:p4runtime_cc_proto",
        "@com_google_absl//absl/container:btree",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/container:flat_hash_set",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_protobuf//:protobuf",
    ],
)

cc_test(
    name = "references_test",
    srcs = ["references_test.cc"],
    deps = [
        "references",
        "@com_google_absl//absl/hash:hash_testing",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "built_ins",
    srcs = [
        "built_ins.cc",
    ],
    hdrs = [
        "built_ins.h",
    ],
    deps = [
        ":ir_cc_proto",
        "//gutil/gutil:status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:string_view",
    ],
)

cc_test(
    name = "built_ins_test",
    srcs = [
        "built_ins_test.cc",
    ],
    deps = [
        ":built_ins",
        ":ir_cc_proto",
        "//gutil/gutil:status_matchers",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "reference_annotations",
    srcs = [
        "reference_annotations.cc",
    ],
    hdrs = [
        "reference_annotations.h",
    ],
    deps = [
        ":built_ins",
        ":ir_cc_proto",
        "//gutil/gutil:collections",
        "//gutil/gutil:status",
        "//p4_pdpi/internal:ordered_map",
        "//p4_pdpi/utils:annotation_parser",
        "@com_github_p4lang_p4runtime//:p4info_cc_proto",
        "@com_google_absl//absl/container:btree",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:string_view",
        "@com_google_protobuf//:protobuf",
    ],
)

cc_test(
    name = "reference_annotations_test",
    srcs = [
        "reference_annotations_test.cc",
    ],
    deps = [
        ":ir_cc_proto",
        ":reference_annotations",
        "//gutil/gutil:proto_matchers",
        "//gutil/gutil:status_matchers",
        "//gutil/gutil:testing",
        "//p4_pdpi/testing:test_p4info_cc",
        "//p4rt_app/utils:ir_builder",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:string_view",
        "@com_google_googletest//:gtest_main",
        "@com_google_protobuf//:protobuf",
    ],
)

cc_library(
    name = "sequencing_util",
    srcs = ["sequencing_util.cc"],
    hdrs = ["sequencing_util.h"],
    # Disable default arguments internally. Using them in PDPI itself is very likely a bug.
    local_defines = ["PDPI_DISABLE_TRANSLATION_OPTIONS_DEFAULT"],
    deps = [
        ":ir_cc_proto",
        "//gutil/gutil:collections",
        "@com_github_p4lang_p4runtime//:p4runtime_cc_proto",
        "@com_google_absl//absl/container:btree",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/types:span",
    ],
)

cc_test(
    name =
        "sequencing_util_test",
    srcs = ["sequencing_util_test.cc"],
    deps = [
        "sequencing_util",
        "@com_google_absl//absl/hash:hash_testing",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "sequencing",
    srcs = ["sequencing.cc"],
    hdrs = ["sequencing.h"],
    # Disable default arguments internally. Using them in PDPI itself is very likely a bug.
    local_defines = ["PDPI_DISABLE_TRANSLATION_OPTIONS_DEFAULT"],
    deps = [
        ":ir_cc_proto",
        ":names",
        ":references",
        ":sequencing_util",
        "//gutil/gutil:collections",
        "//gutil/gutil:status",
        "@boost//:bimap",
        "@boost//:graph",
        "@com_github_google_glog//:glog",
        "@com_github_grpc_grpc//:grpc++",
        "@com_github_p4lang_p4runtime//:p4info_cc_proto",
        "@com_github_p4lang_p4runtime//:p4runtime_cc_proto",
        "@com_google_absl//absl/algorithm:container",
        "@com_google_absl//absl/base:core_headers",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/container:flat_hash_set",
        "@com_google_absl//absl/functional:function_ref",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/types:span",
        "@com_google_protobuf//:protobuf",
    ],
)

cc_library(
    name = "pd",
    srcs = ["pd.cc"],
    hdrs = ["pd.h"],
    # Disable default arguments internally. Using them in PDPI itself is very likely a bug.
    local_defines = ["PDPI_DISABLE_TRANSLATION_OPTIONS_DEFAULT"],
    deps = [
        ":ir",
        ":ir_cc_proto",
        ":translation_options",
        "//gutil/gutil:collections",
        "//gutil/gutil:proto",
        "//gutil/gutil:status",
        "//p4_pdpi/internal:ordered_map",
        "//p4_pdpi/string_encodings:hex_string",
        "//p4_pdpi/utils:ir",
        "@com_github_grpc_grpc//:grpc++",
        "@com_github_p4lang_p4runtime//:p4info_cc_proto",
        "@com_github_p4lang_p4runtime//:p4runtime_cc_proto",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/container:flat_hash_set",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/types:span",
        "@com_google_googleapis//google/rpc:code_cc_proto",
        "@com_google_googleapis//google/rpc:status_cc_proto",
        "@com_google_protobuf//:protobuf",
    ],
)

cc_binary(
    name = "pdgen",
    srcs = ["pdgen.cc"],
    deps = [
        ":ir",
        ":ir_cc_proto",
        ":pdgenlib",
        "//gutil/gutil:proto",
        "//gutil/gutil:status",
        "@com_github_p4lang_p4runtime//:p4info_cc_proto",
        "@com_google_absl//absl/flags:flag",
        "@com_google_absl//absl/flags:parse",
        "@com_google_absl//absl/flags:usage",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
    ],
)

cc_library(
    name = "pdgenlib",
    srcs = ["pdgenlib.cc"],
    hdrs = ["pdgenlib.h"],
    # Disable default arguments internally. Using them in PDPI itself is very likely a bug.
    local_defines = ["PDPI_DISABLE_TRANSLATION_OPTIONS_DEFAULT"],
    deps = [
        ":ir_cc_proto",
        ":pd",
        "//gutil/gutil:collections",
        "//gutil/gutil:status",
        "//p4_pdpi/internal:ordered_map",
        "@com_github_p4lang_p4runtime//:p4info_cc_proto",
        "@com_google_absl//absl/container:flat_hash_set",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
    ],
)

cc_library(
    name = "p4info_union_lib",
    srcs = ["p4info_union_lib.cc"],
    hdrs = ["p4info_union_lib.h"],
    # Disable default arguments internally. Using them in PDPI itself is very likely a bug.
    local_defines = ["PDPI_DISABLE_TRANSLATION_OPTIONS_DEFAULT"],
    deps = [
        "//gutil/gutil:proto",
        "@com_github_p4lang_p4runtime//:p4info_cc_proto",
        "@com_github_p4lang_p4runtime//:p4types_cc_proto",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/types:optional",
        "@com_google_absl//absl/types:span",
        "@com_google_protobuf//:protobuf",
    ],
)

cc_test(
    name = "p4info_union_test",
    srcs = ["p4info_union_test.cc"],
    deps = [
        ":p4info_union_lib",
        "//gutil/gutil:proto_matchers",
        "//gutil/gutil:status_matchers",
        "//gutil/gutil:testing",
        "@com_github_p4lang_p4runtime//:p4info_cc_proto",
        "@com_github_p4lang_p4runtime//:p4types_cc_proto",
        "@com_google_absl//absl/strings",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_binary(
    name = "p4info_union",
    srcs = ["p4info_union.cc"],
    deps = [
        ":p4info_union_lib",
        "//gutil/gutil:proto",
        "@com_github_p4lang_p4runtime//:p4info_cc_proto",
        "@com_google_absl//absl/flags:flag",
        "@com_google_absl//absl/flags:parse",
        "@com_google_absl//absl/flags:usage",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
    ],
)

cc_library(
    name = "ir",
    srcs = ["ir.cc"],
    hdrs = ["ir.h"],
    # Disable default arguments internally. Using them in PDPI itself is very likely a bug.
    local_defines = ["PDPI_DISABLE_TRANSLATION_OPTIONS_DEFAULT"],
    deps = [
        ":built_ins",
        ":ir_cc_proto",
        ":reference_annotations",
        ":translation_options",
        "//gutil/gutil:collections",
        "//gutil/gutil:proto",
        "//gutil/gutil:status",
        "//p4_pdpi/utils:ir",
        "@com_github_grpc_grpc//:grpc++",
        "@com_github_p4lang_p4runtime//:p4info_cc_proto",
        "@com_github_p4lang_p4runtime//:p4runtime_cc_proto",
        "@com_github_p4lang_p4runtime//:p4types_cc_proto",
        "@com_google_absl//absl/algorithm:container",
        "@com_google_absl//absl/base:core_headers",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/container:flat_hash_set",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/types:optional",
        "@com_google_absl//absl/types:span",
        "@com_google_googleapis//google/rpc:code_cc_proto",
        "@com_google_googleapis//google/rpc:status_cc_proto",
        "@com_google_protobuf//:protobuf",
    ],
)

cc_test(
    name = "ir_to_ir_test",
    srcs = ["ir_to_ir_test.cc"],
    deps = [
        ":ir",
        "//gutil/gutil:proto_matchers",
        "//gutil/gutil:status_matchers",
        "//gutil/gutil:testing",
        "@com_google_googletest//:gtest_main",
    ],
)

proto_library(
    name = "ir_proto",
    srcs = ["ir.proto"],
    deps = [
        "@com_github_p4lang_p4runtime//:p4info_proto",
        "@com_github_p4lang_p4runtime//:p4runtime_proto",
        "@com_google_googleapis//google/rpc:code_proto",
        "@com_google_googleapis//google/rpc:status_proto",
    ],
)

proto_library(
    name = "p4_runtime_session_extras_proto",
    srcs = ["p4_runtime_session_extras.proto"],
    deps = [
        "@com_github_p4lang_p4runtime//:p4runtime_proto"
    ],
)

cc_proto_library(
    name = "p4_runtime_session_extras_cc_proto",
    deps = [":p4_runtime_session_extras_proto"],
)

cc_proto_library(
    name = "ir_cc_proto",
    deps = [":ir_proto"],
)

cc_library(
    name = "p4_runtime_session",
    srcs = [
        "p4_runtime_session.cc",
    ],
    hdrs = [
        "p4_runtime_session.h",
    ],
    deps = [
        ":entity_keys",
        ":ir",
        ":ir_cc_proto",
        ":names",
        ":sequencing",
        "//gutil/gutil:collections",
        "//gutil/gutil:status",
        "//gutil/gutil:version",
        "//sai_p4/fixed:p4_roles",
        "//sai_p4/instantiations/google:p4_versions",
        "//thinkit:switch",
        "@com_github_google_glog//:glog",
        "@com_github_grpc_grpc//:grpc++",
        "@com_github_grpc_grpc//:grpc_security_base",
        "@com_github_p4lang_p4runtime//:p4runtime_cc_grpc",
        "@com_github_p4lang_p4runtime//:p4runtime_cc_proto",
        "@com_google_absl//absl/algorithm:container",
        "@com_google_absl//absl/base:core_headers",
        "@com_google_absl//absl/functional:any_invocable",
        "@com_google_absl//absl/numeric:int128",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/synchronization",
        "@com_google_absl//absl/time",
        "@com_google_absl//absl/types:optional",
        "@com_google_absl//absl/types:span",
    ],
)

cc_library(
    name = "pi",
    srcs = ["pi.cc"],
    hdrs = ["pi.h"],
    # Disable default arguments internally. Using them in PDPI itself is very likely a bug.
    local_defines = ["PDPI_DISABLE_TRANSLATION_OPTIONS_DEFAULT"],
    deps = [
        ":ir_cc_proto",
        "//gutil/gutil:collections",
        "//gutil/gutil:status",
        "//p4_pdpi/utils:ir",
        "@com_github_google_glog//:glog",
        "@com_github_p4lang_p4runtime//:p4info_cc_proto",
        "@com_github_p4lang_p4runtime//:p4runtime_cc_proto",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_protobuf//:protobuf",
    ],
)

cc_library(
    name = "ir_properties",
    srcs = ["ir_properties.cc"],
    hdrs = ["ir_properties.h"],
    deps = [
        ":ir_cc_proto",
        "@com_github_p4lang_p4runtime//:p4info_cc_proto",
    ],
)

cc_test(
    name = "ir_properties_test",
    srcs = ["ir_properties_test.cc"],
    deps = [
        ":ir_properties",
        "//gutil/gutil:testing",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "ir_tools",
    srcs = ["ir_tools.cc"],
    hdrs = ["ir_tools.h"],
    # Disable default arguments internally. Using them in PDPI itself is very likely a bug.
    local_defines = ["PDPI_DISABLE_TRANSLATION_OPTIONS_DEFAULT"],
    deps = [
        ":ir_cc_proto",
        "//gutil/gutil:collections",
        "@com_github_p4lang_p4runtime//:p4info_cc_proto",
        "@com_github_p4lang_p4runtime//:p4types_cc_proto",
        "@com_google_absl//absl/functional:any_invocable",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/types:variant",
    ],
)

cc_test(
    name = "ir_tools_test",
    srcs = ["ir_tools_test.cc"],
    deps = [
        ":ir_cc_proto",
        ":ir_tools",
        ":pd",
        "//gutil/gutil:proto",
        "//gutil/gutil:proto_matchers",
        "//gutil/gutil:status_matchers",
        "//gutil/gutil:testing",
        "//p4_pdpi/testing:main_p4_pd_cc_proto",
        "//p4_pdpi/testing:test_p4info_cc",
        "@com_github_p4lang_p4runtime//:p4types_cc_proto",
        "@com_google_absl//absl/container:flat_hash_set",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "p4_runtime_session_extras",
    srcs = ["p4_runtime_session_extras.cc"],
    hdrs = ["p4_runtime_session_extras.h"],
    deps = [
        ":entity_keys",
        ":ir",
        ":ir_cc_proto",
        ":p4_runtime_session",
        ":p4_runtime_session_extras_cc_proto",
        ":pd",
        "@com_github_google_glog//:glog",
        "//gutil/gutil:proto",
        "//gutil/gutil:status",
        "@com_github_p4lang_p4runtime//:p4info_cc_proto",
        "@com_github_p4lang_p4runtime//:p4runtime_cc_proto",
        "@com_google_absl//absl/algorithm:container",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/types:span",
        "@com_google_protobuf//:protobuf",
    ],
)

cc_library(
    name = "p4_runtime_matchers",
    testonly = True,
    hdrs = ["p4_runtime_matchers.h"],
    deps = [
        "//gutil/gutil:proto_matchers",
        "//p4_pdpi/packetlib",
        "//p4_pdpi/packetlib:packetlib_cc_proto",
        "@com_github_p4lang_p4runtime//:p4runtime_cc_proto",
        "@com_google_absl//absl/strings",
        "@com_google_googletest//:gtest",
    ],
)

cc_test(
    name = "p4_runtime_matchers_test",
    srcs = ["p4_runtime_matchers_test.cc"],
    deps = [
        ":p4_runtime_matchers",
        "//gutil/gutil:proto_matchers",
        "//gutil/gutil:testing",
        "//p4_pdpi/packetlib",
        "@com_github_p4lang_p4runtime//:p4runtime_cc_proto",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "translation_options",
    hdrs = ["translation_options.h"],
    deps = ["@com_google_absl//absl/strings:str_format"],
)

cc_library(
    name = "entity_keys",
    srcs = ["entity_keys.cc"],
    hdrs = ["entity_keys.h"],
    deps = [
        "@com_github_google_glog//:glog",
        "//gutil/gutil:status",
        "@com_github_p4lang_p4runtime//:p4runtime_cc_proto",
        "@com_google_absl//absl/hash",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_protobuf//:protobuf",
    ],
)

cc_test(
    name = "entity_keys_test",
    srcs = ["entity_keys_test.cc"],
    deps = [
        ":entity_keys",
        "//gutil/gutil:proto",
        "//gutil/gutil:status_matchers",
        "@com_google_absl//absl/hash:hash_testing",
        "@com_google_absl//absl/strings",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "names",
    srcs = ["names.cc"],
    hdrs = ["names.h"],
    deps = [
        ":built_ins",
        ":ir_cc_proto",
        "//gutil/gutil:collections",
        "//gutil/gutil:status",
        "@com_github_p4lang_p4runtime//:p4info_cc_proto",
        "@com_github_p4lang_p4runtime//:p4runtime_cc_proto",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
    ],
)

cc_test(
    name = "names_test",
    srcs = ["names_test.cc"],
    deps = [
        ":built_ins",
        ":ir_cc_proto",
        ":names",
        "//gutil/gutil:status_matchers",
        "//gutil/gutil:testing",
        "//p4_pdpi/testing:test_p4info_cc",
        "@com_github_p4lang_p4runtime//:p4runtime_cc_proto",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_test(
    name = "ternary_test",
    srcs = ["ternary_test.cc"],
    deps = [
        ":ternary",
        "//p4_pdpi/netaddr:ipv4_address",
        "//p4_pdpi/netaddr:ipv6_address",
        "//p4_pdpi/netaddr:mac_address",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "ternary",
    hdrs = ["ternary.h"],
    deps = [
        "//p4_pdpi/netaddr:ipv4_address",
        "//p4_pdpi/netaddr:ipv6_address",
        "//p4_pdpi/netaddr:mac_address",
    ],
)
